﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Core.Distribution;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.AppVersion;
using FwProject.Core.Car;
using FwProject.Car;
using FwProject.Core.FactoryInfo;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.TypeNumber;
using System.Linq;
using FwProject.Restaurant.Dto;
using FwProject.Distribution.Dto;
using Castle.Core.Logging;
using Abp.AutoMapper;
using Abp.ObjectMapping;
using Microsoft.EntityFrameworkCore;
using FwProject.Restaurant;
using FwProject.Authorization.Users;
using FwProject.Comm;
using FwProject.Settlement.Dto;

namespace FwProject.RemoteEventBus
{
    /// <summary>
    ///餐厅缓存数据
    /// </summary>
    [RemoteEventHandler(ForType = "ModelCatch", ForTopic = "SystemCatch")]
    public class RemoteEventBusCatch : IRemoteEventHandler, ITransientDependency
    {
        public ICacheManager _catchmanager;
        public ILogger iLogger { get; set; }
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        private readonly IRepository<AppVersionManger, Guid> _repositoryAppVersionManger;
        private readonly IRepository<CarInfo, Guid> _repositoryCarInfo;
        private readonly IRepository<CarMap, Guid> _repositoryCarMap;
        private readonly IRepository<DistributionInfoDetails, Guid> _repositoryDistributionInfoDetails;
        private readonly IRepository<FactoryNameInfo, Guid> _repositoryFactoryNameInfo;
        private readonly IRepository<LoadCar, Guid> _repositoryLoadCar;
        private readonly IRepository<LoadingCarInfo, Guid> _repositoryLoadingCarInfo;
        private readonly IRepository<RestaurantInfo, Guid> _repositoryRestaurantInfo;
        private readonly IRepository<RestaurantTypenNumberInfo, Guid> _repositoryRestaurantTypenNumberInfo;
        private readonly IRepository<RestaurantUserInfo, Guid> _repositoryRestaurantUserInfo;
        private readonly IRepository<SettlementInfo, Guid> _repositorySettlementInfo;
        private readonly IRepository<TypeNumberInfo, Guid> _repositoryTypeNumberInfo;
        public readonly IRepository<DepositInfo, Guid> _repositoryDeposit;

        public  IObjectMapper _objectMapper { get; set; }

        public RemoteEventBusCatch(
        ICacheManager catchmanager,
        IUnitOfWorkManager unitOfWorkManager,
        IRepository<DistributionInfo, Guid> repositoryDistributionInfo,
        IRepository<AppVersionManger, Guid> repositoryAppVersionManger,
        IRepository<CarInfo, Guid> repositoryCarInfo,
        IRepository<CarMap, Guid> repositoryCarMap,
        IRepository<DistributionInfoDetails, Guid> repositoryDistributionInfoDetails,
        IRepository<FactoryNameInfo, Guid> repositoryFactoryNameInfo,
        IRepository<LoadCar, Guid> repositoryLoadCar,
        IRepository<LoadingCarInfo, Guid> repositoryLoadingCarInfo,
        IRepository<RestaurantInfo, Guid> repositoryRestaurantInfo,
        IRepository<RestaurantTypenNumberInfo, Guid> repositoryRestaurantTypenNumberInfo,
        IRepository<RestaurantUserInfo, Guid> repositoryRestaurantUserInfo,
        IRepository<SettlementInfo, Guid> repositorySettlementInfo,
        IRepository<TypeNumberInfo, Guid> repositoryTypeNumberInfo,
        IRepository<DepositInfo, Guid> repositoryDeposit
            )
        {
            _catchmanager = catchmanager;
            _unitOfWorkManager = unitOfWorkManager;
            _repositoryDistributionInfo = repositoryDistributionInfo;
            _repositoryAppVersionManger = repositoryAppVersionManger;
            _repositoryCarInfo = repositoryCarInfo;
            _repositoryCarMap = repositoryCarMap;
            _repositoryDistributionInfoDetails = repositoryDistributionInfoDetails;
            _repositoryFactoryNameInfo = repositoryFactoryNameInfo;
            _repositoryLoadCar = repositoryLoadCar;
            _repositoryLoadingCarInfo = repositoryLoadingCarInfo;
            _repositoryRestaurantInfo = repositoryRestaurantInfo;
            _repositoryRestaurantTypenNumberInfo = repositoryRestaurantTypenNumberInfo;
            _repositoryRestaurantUserInfo = repositoryRestaurantUserInfo;
            _repositorySettlementInfo = repositorySettlementInfo;
            _repositoryTypeNumberInfo = repositoryTypeNumberInfo;
            _repositoryDeposit = repositoryDeposit;
            iLogger = NullLogger.Instance;
            _objectMapper = NullObjectMapper.Instance;

        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            ICache cache = _catchmanager.GetCache(eventArgs.EventData.Data[RemoteTypeHelp.Model].ToString());
            iLogger.Info($"缓存名称==={eventArgs.EventData.Data["model"].ToString()}===缓存处理");
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                try
                {

                    ///数据有更改清除视图缓存
                    cache = _catchmanager.GetCache(nameof(AppRestaurantDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(ReceivableDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(DistributionViewDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(LodingDetailsDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(LoadCarDetailsOutputDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(DistributionDetailsView));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(DistributionDetailsListView));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(RestaurantTypeNumberViewDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(AppTopRestaurantDto));
                    cache.Clear();
                    cache = _catchmanager.GetCache(nameof(SettlementInfoDto));
                    cache.Clear();
                    //switch (eventArgs.EventData.Data["model"].ToString())
                    //{
                    //    //case nameof(DistributionInfo):
                    //    //    cache.Set("list", _repositoryDistributionInfo.GetAllIncluding(o => o.DistributionInfoListDetails, o => o.CarInfoModel).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(AppVersionManger):
                    //    //    cache.Set("list", _repositoryAppVersionManger.GetAllList());
                    //    //    break;
                    //    //case nameof(CarInfo):
                    //    //    cache.Set("list", _repositoryCarInfo.GetAllIncluding(o => o.CarUser).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(CarMap):
                    //    //    cache.Set("list", _repositoryCarMap.GetAllIncluding(o => o.CarInfoModel).Where(o=> EF.Functions.DateDiffDay(o.CreationTime, DateTime.Now) == 0).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(DistributionInfoDetails):
                    //    //    cache.Set("list", _repositoryDistributionInfoDetails.GetAllIncluding(o => o.RestaurantTypenNumberInfoModel, o => o.RestaurantInfoModel).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(FactoryNameInfo):
                    //    //    cache.Set("list", _repositoryFactoryNameInfo.GetAllList().OrderByDescending(m => m.CreationTime));
                    //    //    break;
                    //    //case nameof(LoadCar):
                    //    //    cache.Set("list", _repositoryLoadCar.GetAllIncluding(o => o.CarInfoModel, o => o.LoadingCarInfoList).OrderByDescending(m => m.CreationTime).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(LoadingCarInfo):
                    //    //    cache.Set("list", _repositoryLoadingCarInfo.GetAllIncluding(o => o.TypeNumberInfoModel, o => o.LoadCarModel).OrderByDescending(m => m.CreationTime).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //    //case nameof(RestaurantInfo):
                    //    //    cache.Set("list", _repositoryRestaurantInfo.GetAllIncluding(o => o.RestaurantTypenNumberInfos, o => o.EmployeeDriver).OrderByDescending(m => m.CreationTime).OrderByDescending(m => m.CreationTime).ToList());

                    //    //    break;
                    //    //case nameof(RestaurantTypenNumberInfo):
                    //    //    cache.Set("list", _repositoryRestaurantTypenNumberInfo.GetAllIncluding(o => o.TypeNumberInfoModel).OrderByDescending(m => m.CreationTime).ToList());

                    //    //    break;
                    //    //case nameof(RestaurantUserInfo):
                    //    //    cache.Set("list", _repositoryRestaurantUserInfo.GetAllIncluding(O => O.EmployeeRestauranUser, O => O.RestaurantInfoModel).OrderByDescending(m => m.CreationTime).ToList());

                    //    //    break;
                    //    //case nameof(SettlementInfo):
                    //    //    cache.Set("list", _repositorySettlementInfo.GetAllIncluding(m => m.RestaurantInfoModel, m => m.EmployeeDriverDao).OrderByDescending(m => m.CreationTime).ToList());

                    //    //    break;
                    //    //case nameof(TypeNumberInfo):
                    //    //    cache.Set("list", _repositoryTypeNumberInfo.GetAllList().OrderByDescending(m => m.CreationTime));
                    //    //    break;
                    //    //case nameof(DepositInfo):
                    //    //    cache.Set("list", _repositoryDeposit.GetAllIncluding(o=>o.RestaurantInfoModel).OrderByDescending(m => m.CreationTime).ToList());
                    //    //    break;
                    //}
                }
                catch (Exception exe)
                {
                    iLogger.Error($"缓存名称==={eventArgs.EventData.Data["model"].ToString()}===缓存生成错误", exe);
                }
                finally {

                    iLogger.Info($"缓存名称==={eventArgs.EventData.Data["model"].ToString()}");
                }
               
                unitOfWork.Complete();
            }
          
            
        }
    }
}
